// pages/my/index.js

const {
  myApi
} = require('../../../api/index')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 头部安全距离
    tagListT: ['我的模型', '我的收藏', '我的训练'],
    tagActiveT: 0,
    IMGBASEURL: app.globalData.IMGBASEURL,
    dynamicStyle: 0, //头部安全距离
    dayStyle: 0, //用户头部定位后的安全距离
    isTitle: false, //滑动后头部的变化
    tabTop: 0, //tab的粘性定位距离
    draw: false,
    drawActive: false,
    active: 1,
    tabList: ['TA的灵感', 'TA的收藏', 'TA的模型'],
    isLoge: false,
    welfareMessge: {},
    user: {}, //用户信息
    attentionFansCollect: {}, //或获赞数
    PageIndex: 1,
    pageCount: 10,
    listData: [],
    leftList: [],
    rightList: [],
    lodding: false,
    id: "",
    visible: false,
    checkboxActive: "/assets/login/checkbox_def.svg",
    signActive: 1,
    signList: {},
    vipGrade: "",
    imgVip: [
      "/assets/my/yhdj_A1@2x.png",
      "/assets/my/yhdj_A2@2x.png",
      "/assets/my/yhdj_A3@2x.png",
      "/assets/my/yhdj_A4@2x.png",
      "/assets/my/yhdj_A5@2x.png",
      "/assets/my/yhdj_A6@2x.png",
      "/assets/my/yhdj_A7@2x.png",
    ],
    identity: "",
    identityImg: {
      "普通会员": 'icon_vip_normal-24@3x',
      "月度会员": 'icon_vip_month-24@3x',
      "季度会员": 'icon_vip_quarter-24@3x',
      "年度会员": 'icon_vip_year-24@3x',
      "体验会员": 'icon_vip_tryout-24@3x'
    },
    joiAssociationData: true,
    presentInspiration: 0
  },

  loadMyModelList(e) {
    const index = e.currentTarget.dataset['index']
    this.setData({
      tagActiveT: index,
      listData: [],
      PageIndex: 1
    })
    this.init(3)

  },
  eventName(e) {
    this.setData({
      draw: e.detail
    })
  },

  // vip图片筛选
  vipGradeFun(value) {
    switch (value) {
      case "VIP001":
        return 0;
      case "VIP002":
        return 1;
      case "VIP003":
        return 2;
      case "VIP004":
        return 3;
      case "VIP005":
        return 4;
      case "VIP006":
        return 5;
      case "VIP007":
        return 6;
    }
  },
  // vip图片等级
  identityFun(value) {
    console.log(value);
    switch (value) {
      case "普通会员":
        return 0;
      case "月度会员":
        return 1;
      case "季度会员":
        return 2;
      case "年度会员":
        return 3;
    }
  },

  onPullDownRefresh() {
    if (this.data.active == 1) {
      this.setData({
        tab1: "tab-item tab-left tab-active",
        tab2: "tab-item",
        listData: [],
        leftList: [],
        rightList: [],
        PageIndex: 1,
        active: 1
      })
      this.init(1)
    } else if (this.data.active == 2) {
      this.setData({
        tab1: "tab-item tab-left ",
        tab2: "tab-item tab-active",
        listData: [],
        leftList: [],
        rightList: [],
        PageIndex: 1,
        active: 2
      })
      this.init(2)
    }
  },

  // tab栏
  tabActive(e) {
    let index = e.currentTarget.dataset['value']
    this.setData({
      listData: [],
      leftList: [],
      rightList: [],
      PageIndex: 1,
      active: index
    })
    this.init(index)
  },

  // 查看列表详情
  particularsItem(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url.coverImage, // 当前显示图片的http链接
      urls: e.currentTarget.dataset.url.images // 需要预览的图片http链接列表
    })
    return
  },

  splitArray(arr) {
    const mid = Math.ceil(arr.length / 2);
    const firstHalf = arr.slice(0, mid);
    const secondHalf = arr.slice(mid);
    return [firstHalf, secondHalf];
  },
  // 1 我的创作 2 我的收藏
  async init(value) {
    let cIndex = this.data.tagActiveT
    let thet = this
    let LoadingTime= new Date()
    thet.setData({
      lodding: false
    })
    if (value == 1) {
      myApi.querybyid({
        ID: this.data.user.UserId,
        LoadingTime,
        PageIndex: thet.data.PageIndex,
        PageSize: 10,
      }).then((res => {
        console.log(res);
        thet.setData({
          listData: [...thet.data.listData, ...res.items],
          PageIndex: thet.data.PageIndex + 1
        })
        if (thet.data.listData.length >= res.totalCount) {
          thet.setData({
            lodding: true
          })
        }
      }))
    } else if (value == 2) {
      myApi.getmycollectionbypage({
        PageIndex: thet.data.PageIndex,
        PageSize: 10,
        KeyWord: ''
      }).then((res => {
        thet.setData({
          listData: [...thet.data.listData, ...res.Result.Datas],
          PageIndex: thet.data.PageIndex + 1
        })
        const rres = this.splitArray(res.Result.Datas)
        if (this.data.listData.length > 0) {
          this.setData({
            leftList: [...this.data.leftList, ...rres[0]],
            rightList: [...this.data.rightList, ...rres[1]],
          })
        }
        if (thet.data.listData.length >= res.Result.TotalCount) {
          thet.setData({
            lodding: true
          })
        }
      }))
    } else {
      let res
      if (cIndex == 0) {
        res = await myApi.getMyworlLlist({
          "bottomMoldType": "",
          "name": "",
          "PageIndex": thet.data.PageIndex,
          "PageSize": 10
        })
      }
      if (cIndex == 1) {
        res = await myApi.getaicollectionList({
          "PageIndex": this.data.PageIndex,
          "PageSize": 10
        })
      }
      if (cIndex == 2) {
        res = await myApi.getList({
          "PageIndex": this.data.PageIndex,
          "PageSize": 10
        })
      }

      this.setData({
        listData: [...this.data.listData, ...res.items],
        PageIndex: this.data.PageIndex + 1
      })
      if (this.data.listData.length >= res.totalCount) {
        this.setData({
          lodding: true
        })
      }
    }
    wx.stopPullDownRefresh();
  },
  onerror(e) {
    let user = this.data.user
    user.Avatar = this.data.IMGBASEURL + "user_64.png"
    this.setData({
      user: user
    })
  },
  readRend(id) {
    const arr = this.data.listData.filter((e) => e.ID != id)
    const rres = this.splitArray(arr)
    this.setData({
      leftList: rres[0],
      rightList: rres[1],
    })
  },

  // 点击不在提醒
  remind() {
    if (this.data.checkboxActive == "../../assets/login/checkbox_def.svg") {
      this.setData({
        checkboxActive: "../../assets/login/checkbox_sel.svg"
      })
    } else {
      this.setData({
        checkboxActive: "../../assets/login/checkbox_def.svg"
      })
    }
  },


  // 加入社群
  joiAssociation() {
    wx.navigateTo({
      url: './association/association',
    })
  },
  // 删除加入社群
  joiAssociationDelete() {
    this.setData({
      joiAssociationData: false
    })
  },
  succeedClose() {
    this.setData({
      drawActive: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    let _this = this
    let {
      id
    } = options
    this.setData({user:{UserId:id}})
    myApi.getuserinformation({
      UserId: id
    }).then(res => {
      let user = res.Result
      console.log(user);
      this.setData({
        vipGrade: _this.vipGradeFun(user.VIPLevelCode),
        identity: _this.identityFun(user.VIPLevelName),
      })
      myApi.getcount({
        ID: user.UserId
      }).then((res) => {
        this.setData({
          attentionFansCollect: res.Result
        })
      })
      this.setData({
        user
      })
    })
    this.init(1)
    //  获取安全距离等
    wx.getSystemInfo({
      success: res => {
        let systemInfo = wx.getSystemInfoSync()
        let info = wx.getMenuButtonBoundingClientRect()
        this.setData({
          height: (info.top - systemInfo.statusBarHeight) * 2 + info.height,
          dynamicStyle: (res.statusBarHeight) + "px",
          dayStyle: (info.top - systemInfo.statusBarHeight) * 2 + info.height + res.statusBarHeight + "px",
          tabTop: (info.top - systemInfo.statusBarHeight) * 2 + info.height + res.statusBarHeight + "px"
        })
      },
      fail(err) {
        console.log(err);
      }
    })

  },

  onShareAppMessage(e) {
    let id = e.target.dataset.id
    let title = e.target.dataset.title
    let imageUrl = e.target.dataset.img
    return {
      title,
      imageUrl,
      path: `pages/modelStore/modelDetail/index?id=${id}`,
    }
  },
})